// Camera.cpp
//
// Class for controlling a camera.
//
// Author: Nick Kitten
// 2009

#include "Camera.h"

Camera::Camera( int id, float x, float y, float z, float rx, float ry, float rz )
: PoseableObject( id, x, y, z, rx, ry, rz )
{
	dbMakeCamera( id );
}

Camera::~Camera()
{
	dbDeleteCamera( getID() );
}

void Camera::setPosition(float x, float y, float z)
{
	Object::setPosition( x, y, z );
	int lastActiveCam = activeID;
	makeActive();
	dbPositionCamera( getID(), x, y, z );
	dbSetCurrentCamera( lastActiveCam );
}

void Camera::setRotation(float x, float y, float z)
{
	PoseableObject::setRotation( x, y, z );
	float dx = x - dbCameraAngleX( getID() );
	float dy = y - dbCameraAngleY( getID() );
	float dz = z - dbCameraAngleZ( getID() );

	int lastActiveCam = activeID;
	makeActive();
	dbRotateCamera( getID(), dx, dy, dz );
	dbSetCurrentCamera( lastActiveCam );
}

void Camera::makeActive()
{
	dbSetCurrentCamera( getID() );
	activeID = getID();
}

bool Camera::isActive() const
{
	return getID() == activeID;
}

void Camera::setView( int width, int height, int x, int y )
{
	viewArea.minPoint = Position( x, y );
	viewArea.maxPoint = Position( x + width, y + height );
	dbSetCameraView( getID(), x, y, viewArea.maxPoint.x, viewArea.maxPoint.y );
}
	
Rect Camera::getView() const
{
	return viewArea;
}

void Camera::lookAt( float x, float y, float z )
{
	int lastActiveCam = activeID;
	makeActive();
	dbPointCamera( getID(), x, y, z );
	setRotation( dbCameraAngleX( getID() ), dbCameraAngleY( getID() ), dbCameraAngleZ( getID() ) );
	dbSetCurrentCamera( lastActiveCam );
}

void Camera::setToImage( int ID, int width, int height )
{
	dbSetCameraToImage( getID(), ID, width, height );
}

int Camera::activeID = 0;